<?php
declare (strict_types = 1);

namespace app\controller;


use think\facade\View;
use think\Request;
use think\exception\ValidateException;

class AdminSpecial extends AdminInit
{
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        $mod = new \app\common\model\Content();
        $where["model_id"] = 6;
        $handle_list = $mod
            ->where($where)
            ->order("id DESC")
            ->paginate(
                [
                    "query"=>request()->param()
                ]
            );
        if(\request()->isAjax()){
            return json(["code"=>200,"list"=>$handle_list]);
        }
        View::assign("handle_list",$handle_list);
        View::assign("title","文章列表");
        return  view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

        return \view();
    }



    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
       
        $user = $this->user;
        if(!$user){
            return redirect((string) url('index/index',['redirect' => 'index/user/index']));
        }
        $mod = new \app\common\model\Content();

        $d = $request->post();

        $d["uid"] = $user["id"];
        $d["author"] = $user["nickname"];
        $d["userinfo"] = json_encode($user);

        if(isset($d['status']) && $d["status"] == "on"){
            $d["status"] = 1;
        }


        if(isset($d["id"])){
            //修改时候需要先查后改
            $log = $mod->find($d["id"]);
            $res = $log->save($d);
        }else{
            $res = $mod->save($d);
        }

        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }

  

    

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $mod = new \app\common\model\Content();
        $res = $mod->find($id);
        View::assign("res",$res);
        return view('create');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
        public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {

        $res = \app\common\model\Content::where("id",$id)->find();

        $res->delete();
        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }

    }

}
